/**
 * 可以在页面上显示 分享路径
 */
Rsd.define('Rsd.tool.MarkdownViewer', {
    extend: 'Rsd.container.Dialog',
    requires: [
        'Rsd.form.Button',
        'Rsd.form.Label',
        'Rsd.form.Link',
        'Rsd.form.CheckBox',
        'Rsd.control.Svg',
        'Rsd.control.IframeBox',
        'Rsd.control.List'
    ],
    //
    editorHeight: '100%',
    /**
     * 插件路径
     * plugin/editormd/app/view.html
     */
    src:"",
    /**
     * @description string or object ,string md字符串,object 调用下载接口需要的参数； 优先使用string
     */
    data:null,
     
    /**
     * @description 分享按钮响应事件
     */
    shareHandler:null,
    /**
     * @description 发布按钮响应事件
     */
    publishHandler:null,
    /**
     * 
     */
    items:[
        {
            xtype:'iframe', 
            flex:1,
            //width:'100%',
            height:'100%', 
            src:'',
            sandbox:"allow-same-origin allow-scripts allow-popups allow-forms",
            ctrl:{style:{boder:'1px red solid'}},
            label:{
                position:'bottom',
                space:0,
                xtype:'container',
                header:false,
                height:60,
                style:{display:'flex'},
                layout:'hbox',
                items:[
                    {
                        xtype:'list',
                        label:{position:'left',content:'读者列表'},
                        style:{color: '#999289',textAlign:'left'},
                        height:60,
                        flex:1,
                    }, 
                    {
                        xtype:'button',
                        height:40,
                        width:120,
                        text:'分 享',
                        style:{backgroundColor:'#0A8029',borderColor:'#0A8029'},
                        handler:'_btn_share'
                    },
                    {
                         xtype:'button',
                        height:40,
                        width:120,
                         text:'发 布',
                        style:{backgroundColor:'#6d0c7dd1',borderColor:'#6d0c7dd1'},
                        handler:'_btn_publish'
                    }
                ]
            },
            onload:'iframe_load'
        } 
    ],
    /**
     * 
     * @param {*} config 
     */
    constructor: function MarkdownViewer(config) {
        config = config || {};
        this.apply(config); 
        this.items[0].src = this.src || (Rsd.getRedjsHost()+'plugin/marked/app/viewer.html?h=' + this.editorHeight); 
    },
    /**
     *  
     */
    load:function load()
    {
        var me = this;
        
        me.callParent();  
    },
    /**
     * 
     * @param {*} evt 
     * @returns 
     */
    iframe_load:function iframe_load(evt)
    {
        var me = this;
        //this.log(evt.target.src, me.items[0].src);
       
        var c_win = me.items[0].ctrl.element.contentWindow;
        if(c_win.document.readyState !== "complete")
        {
            console.error('document current state is:' + c_win.document.readyState );
            return;
        }
        if(Rsd.isString(me.data))
        {
            me.render_html(me.data);
            return;
        } 
        if(Rsd.isObject(me.data))
        {
             me.download(me.data);
            return;
        }
    },
    /**
     * 
     * @param {*} key 
     */
    download:function download(args)
    {
        //this.log(this.fileDownloadServiceName,args);
        var _args = args || this.data||{};
        if(Rsd.isEmpty(_args))
        { 
            this.log('file download failure ,args is null');
            return;
        }
        
        var me = this;
        Rsd.app.requestService(this.fileDownloadServiceName,_args, function(resp) {
                    
            if(resp.success)
            {
                //更新当前key
                me.data = _args; 
                me.render_html(resp.data,function(){
                    //处理回调
                }); 
            } 
            
        },'正在加载数据...');
                
               
    },
    
    /**
     * 
     * @param {*} data 
     * @param {*} callback 
     * @returns 
     */
    render_html:function render_html(data,callback)
    {
        var me = this;
        var c_win = me.items[0].ctrl.element.contentWindow;
         
         if(c_win.render)
         {
             c_win.render(data,callback);
             return;
         }
           
         console.error(c_win.location.href + "未实现接口:render");
    },
      
    /**
     * 
     */
    _btn_share: function btn_share() {
        var me = this;
        if (me.shareHandler) {
            me.funApplyByIOC(me.shareHandler, [me.data]);
            return;
        } 

        Rsd.popup('创建一个用于分享的accesstoken'); 
    },
    /**
     * 
     */
    _btn_publish: function btn_publish() {
        var me = this;
        if (me.publishHandler) {

            me.funApplyByIOC(me.publishHandler, [me.data]);
            return;
        }
        
        Rsd.popup('发布html到网站，app,或社交媒体');
    },

});